﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;

namespace TSOL.Helpers
{
    public static class SPFieldHelpers
    {
        #region SPFieldURL 
        /// <summary>
        /// Returns the value of an Url-Field.
        /// </summary>
        public static string GetFieldUrlValue(
            this SPListItem item, string fieldName)
        {
            if (item != null)
            {
                SPFieldUrlValue urlValue =
                    new SPFieldUrlValue(item[fieldName] as string);
                return urlValue.Url;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Returns the description of an Url-Field.
        /// </summary>
        public static string GetFieldUrlDescription(
            this SPListItem item, string fieldName)
        {
            if (item != null)
            {
                SPFieldUrlValue urlValue =
                    new SPFieldUrlValue(item[fieldName] as string);
                return urlValue.Description;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Sets the value of an URL-Field.
        /// </summary>
        public static void SetFieldValueUrl(this SPListItem item,
            string fieldName, string url, string description)
        {
            if (item != null)
            {
                item[fieldName] =
                    new SPFieldUrlValue()
                    {
                        Description = description,
                        Url = url
                    };
            }
        }
        #endregion

        #region SPFieldText

        /// <summary>
        /// Returns the description of an Text-Field.
        /// </summary>
        public static string GetFieldTextValue(
            this SPListItem item, string fieldName)
        {
            if (item != null)
            {
                if (item.Fields.ContainsField(fieldName))
                    return EParser.Parse<string>(item[fieldName]);
                else
                    return default(string);
            }
            else
            {
                return default(string);
            }
        }

        #endregion


        #region SPFieldInteger

        /// <summary>
        /// Returns the description of an Integer-Field.
        /// </summary>
        public static int GetFieldIntegerValue(
            this SPListItem item, string fieldName)
        {
            if (item != null)
            {
                if (item.Fields.ContainsField(fieldName))
                    return EParser.Parse<int>(item[fieldName]);
                else
                    return default(int); 
            }
            else
            {
                return default(int);
            }
        }

        #endregion

        #region SPFieldBoolean

        /// <summary>
        /// Returns the description of an Boolean-Field.
        /// </summary>
        public static bool GetFieldBooleanValue(
            this SPListItem item, string fieldName)
        {
            if (item != null)
            {
                return EParser.Parse<bool>(item[fieldName].ToString());
            }
            else
            {
                return default(bool);
            }
        }

        #endregion


        #region SPFieldChoose

        /// <summary>
        /// Returns the description of an Choose-Field.
        /// </summary>
        public static string GetFieldChooseValue(
            this SPListItem item, string fieldName)
        {
            if (item != null)
            {
                return EParser.Parse<string>(item[fieldName].ToString());
            }
            else
            {
                return default(string);
            }
        }

        #endregion

        #region SPFieldLookup

        /// <summary>
        /// Returns the description of an Choose-Field.
        /// </summary>
        public static string GetFieldLookupValue(
            this SPListItem item, string fieldName)
        {
            if (item != null)
            {
                string[] _valueArray = item[fieldName].ToString().Split(new string[] { ";#" }, StringSplitOptions.None);
                if (_valueArray != null && _valueArray.Length>1) 
                    return _valueArray[1];
                else
                    return default(string);
            }
            else
            {
                return default(string);
            }
        }

        #endregion
    }
}